package com.epsoft.odin7cj.flow.api.history;

import java.util.ArrayList;
import java.util.List;

import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.history.HistoricActivityInstance;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.camunda.bpm.engine.history.HistoricTaskInstance;
import org.camunda.bpm.engine.history.HistoricVariableInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.epsoft.odin7cj.core.dto.RetData;
import com.epsoft.odin7cj.core.enums.CoreDateStyle;
import com.epsoft.odin7cj.core.util.JhkjDateUtil;
import com.epsoft.odin7cj.flow.bpo.task.TaskBPO;
import com.epsoft.odin7cj.flow.model.form.FlowForm;
import com.epsoft.odin7cj.flow.model.instance.FlowInstance;
import com.epsoft.odin7cj.flow.model.task.TaskInfo;

/**
 * DESC: 流程历史信息控制类
 * @author 胡义振  
 * @date 2018年10月10日
 */
@RestController
@RequestMapping("/api/flow/history")
public class HistoryApi {
	
	@Autowired
	private HistoryService historyService;
	@Autowired
	private TaskBPO taskBPO;
	
	/**
	 * DESC: 获取历史实例
	 * @auther 胡义振
	 * @date 2020年6月8日 
	 * @return
	 */
	@RequestMapping("/getHisInstance")
	public RetData<List<FlowInstance>> getHistoricInstance() {
		RetData<List<FlowInstance>> retData = new RetData<List<FlowInstance>>();
		List<FlowInstance> listHisFlowInstance = new ArrayList<FlowInstance>();
		List<HistoricProcessInstance> hisInstatnce = historyService.createHistoricProcessInstanceQuery().orderByProcessInstanceStartTime().asc().list();
		
		for(HistoricProcessInstance proIns : hisInstatnce) {
			FlowInstance flowInstance = new FlowInstance();
			flowInstance.setInstanceId(proIns.getId());
			flowInstance.setProcessInstanceId(proIns.getId());
			flowInstance.setRootProcessInstanceId(proIns.getRootProcessInstanceId());
			flowInstance.setCaseInstanceId(proIns.getCaseInstanceId());
			flowInstance.setBusinessKey(proIns.getBusinessKey());
			flowInstance.setDefinitionId(proIns.getProcessDefinitionId());
			flowInstance.setStartUserId(proIns.getTenantId());
			listHisFlowInstance.add(flowInstance);
		}
		retData.setRetBody(listHisFlowInstance);
		return retData;
	}
	 
	
    /**
     * Desc: 获取历史表单值
     * @return
     * @throws Exception
     */
	@RequestMapping("/getTaskParam")
    public RetData<List<FlowForm>> getReformTask(String taskId) throws Exception {
		RetData<List<FlowForm>> retData = new RetData<List<FlowForm>>();
		List<FlowForm> listFlowForm = new ArrayList<>();
		List<HistoricVariableInstance> list = historyService.createHistoricVariableInstanceQuery().taskIdIn(taskId).list();
		for(HistoricVariableInstance hvi : list) {
			FlowForm flowForm = new FlowForm();
			flowForm.setFieldName(hvi.getName());
			flowForm.setFieldValue(hvi.getValue());
			listFlowForm.add(flowForm);
		}
    	retData.setRetBody(listFlowForm);
    	return retData;
    }
	
    /**
     * Desc: 获取实例的历史任务
     * @return
     * @throws Exception
     */
	@RequestMapping("/getHisTask")
    public RetData<List<TaskInfo>> getHisTask(String instanceId) throws Exception {
		RetData<List<TaskInfo>> retData = new RetData<List<TaskInfo>>();
		List<TaskInfo> listTaskInfo = new ArrayList<>();
		List<HistoricTaskInstance>  htiList = historyService.createHistoricTaskInstanceQuery().processInstanceId(instanceId).finished().list();
		 
		for(HistoricTaskInstance hti : htiList) {
			 
			 TaskInfo taskInfo = new TaskInfo();
			 taskInfo.setTaskId(hti.getId());
			 taskInfo.setTaskName(hti.getName());
			 taskInfo.setInstanceId(instanceId);
			 taskInfo.setTaskOwner(hti.getOwner());
			 taskInfo.setTaskDefinitionKey(hti.getTaskDefinitionKey());
			 taskInfo.setStartTime(JhkjDateUtil.dateToString(hti.getStartTime(), CoreDateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
			 taskInfo.setEndTime(JhkjDateUtil.dateToString(hti.getEndTime(), CoreDateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
			 
			 FlowForm flowForm = taskBPO.getFlowFormByTaskId(hti.getId());
			 taskInfo.setFlowForm(flowForm);
			 
			 List<HistoricVariableInstance> hviList = historyService.createHistoricVariableInstanceQuery().taskIdIn(hti.getId()).list();
			 if(hviList!=null) {
				 List<FlowForm> flowFormList = new ArrayList<>();
				 for(HistoricVariableInstance hvi : hviList) {
					FlowForm tmpForm = new FlowForm();
					tmpForm.setFieldName(hvi.getName());
					tmpForm.setFieldValue(hvi.getValue());
					flowFormList.add(tmpForm);
				 }
				 taskInfo.setFlowFormList(flowFormList);
			 }
			 listTaskInfo.add(taskInfo);
		 }
		 
    	retData.setRetBody(listTaskInfo);
    	return retData;
    }
	
	
}
